package com.yfbao.horizon.compress;

import com.yfbao.horizon.cache.compress.LZ4Compress;
import com.yfbao.horizon.cache.vo.CacheVo;
import jdk.nashorn.internal.ir.debug.ObjectSizeCalculator;
import org.apache.lucene.util.RamUsageEstimator;
import org.openjdk.jol.info.ClassLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class ObjectmMetaStat {

    public static void main(String[] args) {
//        calcList();
//        calcListForCompress();
        calcResourceCompress();
    }

    private static void calcList(){
        List<Long> list =new ArrayList<>();
        Random random =new Random();
        for(int i=0;i<5000;i++){
            list.add(random.nextLong());
        }
        long objectSize = ObjectSizeCalculator.getObjectSize(list);
        System.out.println(objectSize);
//        String printLog = ClassLayout.parseInstance(list).toPrintable();
//        System.out.println(printLog);
//        long totalMemory = ClassLayout.parseInstance(list).getLossesTotal();
//        System.out.println("jolGetTotalMemory getObject memory : " + totalMemory);
        long  totalMemory1 = RamUsageEstimator.sizeOf(list);
        System.out.println("luceneGetTotalMemory getObject memory : " + totalMemory1);
    }

    private static void calcListForCompress(){
        List<Long> list =new ArrayList<>();
        Random random =new Random();
        for(int i=0;i<3000;i++){
            list.add(random.nextLong());
        }
        long objectSize1 = ObjectSizeCalculator.getObjectSize(list);
        System.out.println(objectSize1);
        CacheVo cacheVo = LZ4Compress.compressObject(list);

        long objectSize = ObjectSizeCalculator.getObjectSize(cacheVo);
        System.out.println(objectSize);
        long  totalMemory1 = RamUsageEstimator.sizeOf(cacheVo);
        System.out.println("luceneGetTotalMemory getObject memory : " + totalMemory1);
    }


    private static void calcResourceCompress(){
        List<CtpEntityResource> list =new ArrayList<>();
        Random random =new Random();
        List<String> types =new ArrayList<>();
        types.add("BASE_SET");
        types.add("WORKSPACE");
        types.add("BIZ_HOME");
        types.add("B2Z_HOME");
        types.add("B2Z2_HOME");
        types.add("B2Z4_HOME");
        types.add("B2Z_HOMEsd");
        types.add("B2Z_HOMEd");
        types.add("B2Z_HOMEh");
        types.add("B2Z_HOME1111");
        types.add("B2Z_HOMEmmm");
        for(int i=0;i<4000;i++){
            CtpEntityResource resource = new CtpEntityResource();
            resource.setResourceAppId(UUID.randomUUID().toString());
            resource.setResourceId(random.nextLong());
            resource.setResourceDataType(types.get(i%types.size()));
            resource.setResourceType("MENU");
            resource.setResourceParentId(random.nextLong());
            resource.setResourceUrl(UUID.randomUUID().toString()+UUID.randomUUID().toString()+random.nextLong());
            resource.setResourceOpenType(types.get((i+random.nextInt(10))%types.size()));
            resource.setResourcePath(UUID.randomUUID().toString());
            resource.setResourceIcon(UUID.randomUUID().toString());
            list.add(resource);
        }

        long objectSize1 = ObjectSizeCalculator.getObjectSize(list);
        System.out.println(objectSize1);
        CacheVo cacheVo = LZ4Compress.compressObject(list);

        long objectSize = ObjectSizeCalculator.getObjectSize(cacheVo);
        System.out.println(objectSize);
        long  totalMemory1 = RamUsageEstimator.sizeOf(cacheVo);
        System.out.println("luceneGetTotalMemory getObject memory : " + totalMemory1);
    }
}
